package cn.dgut.training.config;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.concurrent.Executor;

@Slf4j
@Configuration
public class MybatisConfig implements MetaObjectHandler {


    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }


    @Bean(name = "taskExecutor")
    public Executor taskExecutor(){
        ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("userThread-");
        executor.initialize();
        return executor;

    }

    // 新增的时候自动填充
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        Integer addUser = (Integer) getFieldValByName("addUser", metaObject);
        this.setFieldValByName("addTime",DateTime.now(),metaObject);
        this.setFieldValByName("deleteFlag", 0, metaObject);
        this.setFieldValByName("addUser", addUser, metaObject);
    }

    // 更新的税后自动填充
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        Integer updateUser=(Integer)  getFieldValByName("updateUser",metaObject);
        this.setFieldValByName("updateTime",DateTime.now(),metaObject);
        this.setFieldValByName("updateUser", updateUser, metaObject);
    }

    public static void main(String[] args) {
        System.out.println(DateTime.now());
    }
}
